package com.example.pclimb.ui.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pclimb.R;
import com.example.pclimb.model.ClimbRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 陪爬申请列表适配器
 */
public class ClimbRequestAdapter extends RecyclerView.Adapter<ClimbRequestAdapter.RequestViewHolder> {

    private List<ClimbRequest> requests = new ArrayList<>();
    private OnItemClickListener listener;
    private OnActionClickListener actionListener;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    @NonNull
    @Override
    public RequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_climb_request, parent, false);
        return new RequestViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RequestViewHolder holder, int position) {
        ClimbRequest currentRequest = requests.get(position);
        Context context = holder.itemView.getContext();
        
        // 设置地点
        holder.textLocation.setText(currentRequest.getLocation());
        
        // 设置攀登日期
        if (currentRequest.getClimbDate() != null) {
            holder.textDate.setText(dateFormat.format(currentRequest.getClimbDate()));
        } else {
            holder.textDate.setText("日期未设置");
        }
        
        // 设置人数
        holder.textPeopleCount.setText(String.valueOf(currentRequest.getNumberOfPeople()) + "人");
        
        // 设置预算
        holder.textBudget.setText(String.valueOf(currentRequest.getBudget()) + "元");
        
        // 设置需求（如果过长则截断）
        String requirements = currentRequest.getRequirements();
        if (requirements != null && requirements.length() > 50) {
            requirements = requirements.substring(0, 47) + "...";
        }
        holder.textRequirements.setText(requirements);
        
        // 根据状态显示不同的标签和按钮
        switch (currentRequest.getStatus()) {
            case ClimbRequest.STATUS_PENDING:
                holder.textStatus.setText("待匹配");
                holder.textStatus.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_pending));
                holder.btnAction.setText("查看详情");
                holder.btnAction.setVisibility(View.VISIBLE);
                break;
                
            case ClimbRequest.STATUS_MATCHED:
                holder.textStatus.setText("已匹配");
                holder.textStatus.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_matched));
                holder.btnAction.setText("查看详情");
                holder.btnAction.setVisibility(View.VISIBLE);
                break;
                
            case ClimbRequest.STATUS_AWAITING_PAYMENT:
                holder.textStatus.setText("待支付");
                holder.textStatus.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_awaiting_payment));
                holder.btnAction.setText("去支付");
                holder.btnAction.setVisibility(View.VISIBLE);
                break;
                
            case ClimbRequest.STATUS_COMPLETED:
                holder.textStatus.setText("已完成");
                holder.textStatus.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_completed));
                holder.btnAction.setText("去评价");
                holder.btnAction.setVisibility(View.VISIBLE);
                break;
                
            case ClimbRequest.STATUS_CANCELLED:
                holder.textStatus.setText("已取消");
                holder.textStatus.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_cancelled));
                holder.btnAction.setVisibility(View.GONE);
                break;
                
            default:
                holder.textStatus.setText("未知状态");
                holder.btnAction.setVisibility(View.GONE);
                break;
        }
        
        // 设置按钮点击事件
        holder.btnAction.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onActionClick(currentRequest);
            }
        });
    }

    @Override
    public int getItemCount() {
        return requests.size();
    }

    public void setRequests(List<ClimbRequest> requests) {
        this.requests = requests;
        notifyDataSetChanged();
    }

    public ClimbRequest getRequestAt(int position) {
        return requests.get(position);
    }

    class RequestViewHolder extends RecyclerView.ViewHolder {
        private TextView textLocation;
        private TextView textDate;
        private TextView textPeopleCount;
        private TextView textBudget;
        private TextView textRequirements;
        private TextView textStatus;
        private Button btnAction;

        public RequestViewHolder(@NonNull View itemView) {
            super(itemView);
            textLocation = itemView.findViewById(R.id.text_location);
            textDate = itemView.findViewById(R.id.text_date);
            textPeopleCount = itemView.findViewById(R.id.text_people_count);
            textBudget = itemView.findViewById(R.id.text_budget);
            textRequirements = itemView.findViewById(R.id.text_requirements);
            textStatus = itemView.findViewById(R.id.text_status);
            btnAction = itemView.findViewById(R.id.btn_action);

            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(requests.get(position));
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(ClimbRequest request);
    }
    
    public interface OnActionClickListener {
        void onActionClick(ClimbRequest request);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    public void setOnActionClickListener(OnActionClickListener listener) {
        this.actionListener = listener;
    }
} 